package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐服务实现类
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    //注入dao
    @Autowired
    private SetmealDao setmealDao;

    /*@Autowired
    private OrderDao orderDao;*/

    /**
     * 据套餐Id查询检查组Ids
     * @param setmealId
     * @return
     */
    @Override
    public Integer[] findcheckGroupIdsBySetmealId(Integer setmealId) {
        return setmealDao.findcheckGroupIdsBySetmealId(setmealId);
    }

    /**
     * 增加套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //增加套餐基本信息
        setmealDao.add(setmeal);
        //获取自增主键id
        Integer setmealId = setmeal.getId();
        //添加套-组关系
        add_setmeal_checkGroup_relation(setmealId, checkgroupIds);
    }

    /**
     * 套餐分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //分页插件
        //开始分页，入参currentPage, pageSize自动拼接在后面的查询中，xxx机制
        //两行代码紧跟，连注释都不行
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setmealDao.findPage(queryString);
        //返回前端需要的内容，封装成PageResult
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询所有套餐
     * @return
     */
    @Override
    public List<Setmeal> getSetmeal() {
        return setmealDao.getSetmeal();
    }

    /**
     * 根据套餐ID查询完整套餐详情
     * @param setmealId
     * @return
     */
    @Override
    public Setmeal findById(Integer setmealId) {
        return setmealDao.findById(setmealId);
    }


    /**
     * 套餐饼状图统计
     * @return
     */
    @Override
    public Map<String, Object> getSetmealReport() {

        //查询多条所需记录
        List<Map<String, Object>> mapList  =  setmealDao.getSetmealReport();
        //封装转化所需记录
        List<String> setmealNames = new ArrayList<>();
        if (mapList != null) {
            for (Map<String, Object> map : mapList) {
                String name = (String) map.get("name");
                setmealNames.add(name);
            }
        }
        //返回前端需要的记录
        Map<String, Object> map = new HashMap<>();
        map.put("setmealNames",setmealNames);
        map.put("setmealCount", mapList);
        return map;
    }



    /**
     * 抽取方法：添加套-组关系
     * @param setmealId
     * @param checkgroupIds
     */
    public void add_setmeal_checkGroup_relation(Integer setmealId, Integer[] checkgroupIds) {
        Map<String, Integer> map = new HashMap<>();
        if (checkgroupIds!=null && checkgroupIds.length>0) {
            for (Integer checkgroupId : checkgroupIds) {
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                setmealDao.add_setmeal_checkGroup_relation(map);
            }
        }
    }
}
